package com.lihao.sam.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lihao.sam.dao.AccommodationFeeCollectionDao;
import com.lihao.sam.dao.UserDao;

import com.lihao.sam.entity.User;
import com.lihao.sam.entity.xiaobei.AccommodationFeeCollection;
import com.lihao.sam.service.intf.AccommodationFeeCollectionService;
import com.lihao.sam.vo.PageVo;
import com.lihao.sam.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * @author yifu
 * @date2022/11/22 19:51
 */
@Service
public class AccommodationFeeCollectionServiceImpl implements AccommodationFeeCollectionService {

    @Resource
    private AccommodationFeeCollectionDao dao;
    @Override
    public R add(AccommodationFeeCollection accommodationFeeCollection) {
        //1.校验参数
        if(accommodationFeeCollection!=null){
            //2.操作数据库
            if(dao.add(accommodationFeeCollection)>0){
                return R.ok();
            }
        }
        return R.fail();
        }

    @Override
    public R update(AccommodationFeeCollection accommodationFeeCollection) {
        //1.校验参数
        if(accommodationFeeCollection!=null){
            //2.操作数据库
            if(dao.update(accommodationFeeCollection)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R delete(int id) {
        //1.校验参数
        if(id>0){
            //2.操作数据库
            if(dao.delete(id)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R delBatch(int[] array) {
        if (dao.delBatch(array) > 0) {
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public PageVo queryPage(String studentid,String afname, String college, int page, int limit) {
        //1.开启分页，设置对应的页码
        PageHelper.startPage(page,limit);
        //2.开始查询
        PageInfo<AccommodationFeeCollection> pageInfo=new PageInfo<>(dao.selectByWhere(studentid, afname,college));
        //3.返回对应的结果
        return PageVo.ok(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R uploadExcel(MultipartFile file) throws IOException {
        if (file != null) {
            InputStream inputStream = file.getInputStream();
            List<AccommodationFeeCollection> list = EasyExcel.read(inputStream, AccommodationFeeCollection.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                }
            }).sheet().doReadSync();
            if (dao.addBatch(list) > 0) {
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public void exportData(HttpServletResponse response) throws IOException {
    //设置下载的文件名
        String fn = "acc_" + System.currentTimeMillis() + ".xlsx";
        //设置响应消息头，实现文件下载
        response.setHeader("content-disposition", "attachment;filename=" + fn);
        //写出要下载的内容
        EasyExcel.write(response.getOutputStream(),AccommodationFeeCollection.class).sheet().doWrite(dao.selectAll());
    }
    }






